package com.cspg.design.utils;

public class StringSubstringUtil {

    public static String subStringNumPP( String str)
    {
        int last_num = 0 ;
        String sub = "";
        String oldStr = str;
        while (true)
        {
            String last = oldStr.substring(oldStr.length() -1 );
            boolean isNum = last.matches("[0-9]+");
            if (isNum)
            {
                sub += last;
            }
            else
            {
                break;
            }
            oldStr = oldStr.substring(0,oldStr.length()-1);
            if (oldStr.isEmpty())
            {
                break;
            }
        }

        sub = new StringBuilder(sub).reverse().toString();
        String zero = "";
        for (int i =0 ; i<sub.length();i++)
        {
            String first = sub.substring(i,i+1);
            if (Integer.valueOf(first ) == 0)
            {
                zero += "0";
            }
            else
            {
                break;
            }
        }

        if (!sub.trim().isEmpty())
        {
            last_num = Integer.valueOf(sub) +1;
            if (last_num%10 == 0 && zero.length()>0)
            {
                zero = zero.substring(0,zero.length()-1);
            }
            str = oldStr + zero + last_num;
        }
        else
        {
            str = oldStr + zero;
        }


        return  str;
    }
}
